package com.wetao.module_user.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatButton;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.recyclerview.widget.RecyclerView;

import com.wetao.lib_common.bean.response.Loginbean_CoachClass;
import com.wetao.module_user.R;

import org.jetbrains.annotations.NotNull;

import java.util.List;

public class ClassRecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {


    private Context mContext;
    private List<Loginbean_CoachClass> mClassList;

    public ClassRecyclerAdapter(Context context, List<Loginbean_CoachClass> coachClassList) {
        mContext = context;
        mClassList = coachClassList;
    }

    public void setData(List<Loginbean_CoachClass> coachClassList) {
        mClassList = coachClassList;
    }

    @NonNull
    @NotNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.user_adapter_item_class_teach, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull @NotNull RecyclerView.ViewHolder holder, int position) {
        ViewHolder viewHolder = (ViewHolder) holder;

        viewHolder.btExit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(null != mOnItemClickListener) {
                    mOnItemClickListener.onItemClickExit(position, mClassList);
                }
            }
        });

        viewHolder.btSwitch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(null != mOnItemClickListener) {
                    mOnItemClickListener.onItemClickSwitch(position, mClassList);
                }
            }
        });

        Loginbean_CoachClass classInfo = mClassList.get(position);
        StringBuilder sb = new StringBuilder(TextUtils.isEmpty(classInfo.getSchoolName()) ? "": classInfo.getSchoolName()).append("/")
                .append(TextUtils.isEmpty(classInfo.getClassName()) ? "": classInfo.getClassName());

        viewHolder.tvClassTeach.setText(sb.toString());
    }

    @Override
    public int getItemCount() {
        return null == mClassList ? 0 : mClassList.size();
    }


    class ViewHolder extends RecyclerView.ViewHolder {
        private AppCompatTextView tvClassTeach;
        private AppCompatButton btSwitch;
        private AppCompatButton btExit;

        public ViewHolder(View itemView) {
            super(itemView);

            tvClassTeach = itemView.findViewById(R.id.tv_class_teach);
            btSwitch = itemView.findViewById(R.id.bt_switch);
            btExit = itemView.findViewById(R.id.bt_exit);
        }
    }

    public void setOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }
    OnItemClickListener mOnItemClickListener;
    public interface OnItemClickListener {

        void onItemClickSwitch(int position, List<Loginbean_CoachClass> listModel);

        void onItemClickExit(int position, List<Loginbean_CoachClass> listModel);

    }
}
